# cor ranks see: https://www.ncl.ac.uk/webtemplate/ask-assets/external/maths-resources/statistics/regression-and-correlation/strength-of-correlation.html
## Ranking scale: 
# Kumar K. P., Reddi V. Significance of Spearman’s Rank Correlation Coefficient //International Journal For Multidisciplinary Research. – 2023. – Т. 5. – №. 4.
# 0 - 0.19 - very week
# 0.2 - 0.39 - week
# 0.4 - 0.59 - moderate
# 0.6 - 0.79 - strong
# 0.8 - 1.0 very strong

# plot results 
library("raster")
library("sf")
library("rasterVis")
library("RColorBrewer")

# significance: r >= 0.49
mn <- c("январь", "февраль", "март", "апрель", "май", "июнь", "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь")

# iterate by months
for (i in 1:12) {
  df <- read.csv(paste0("output/splited/halfed/cor_monthly_second/", i, '.csv'))
  df$x <- as.numeric(str_replace(df$x, "X", ""))
  df$y <- as.numeric(str_replace(df$y, "X", ""))
  
  drho <- df[,c("x", "y", "rho")]
  dpval <- df[,c("x", "y", "p")]
  
  rsf <- st_as_sf(drho, coords = c("x", "y"), crs=4326)
  psf <- st_as_sf(dpval, coords = c("x", "y"), crs=4326)
  
  rs <- raster::rasterize(rsf, raster::raster(rsf, resolution = 0.11), rsf$rho)
  ps <- raster::rasterize(psf, raster::raster(psf, resolution = 0.11), psf$p)
  
  at <- c(-1, -0.8, -0.6, -0.49, 0.49, 0.6, 0.8, 1)
  cc <- list(at=at, ## where the colors change
             labels=list(
               at=at## where to print labels
             ))
  png(paste0("output/splited/halfed/mapcor-second/", i, ".png"), res=180, width=1200, height=900)
  print({
    levelplot(rs,  contour = T, region = T, margin = F, main = mn[i], par.settings = BuRdTheme, at=at, colorkey = cc)
  })
  dev.off()
}
